<!-- eslint-disable vue/html-self-closing -->
<template>
  <div class="upload-img">
    <el-upload
      accept="image/png, image/jepg"
      :class="{ disabled: value }"
      action="#"
      :limit="1"
      list-type="picture-card"
      :file-list="fileList"
      :on-remove="handelRemove"
      :on-preview="handelDiaShow"
      :http-request="handelUpload"
    >
      <!-- <img v-if="value" :src="value" class="avatar" /> -->
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 图片预览弹框 -->
    <el-dialog :visible.sync="dialogVisible" title="查看头像" top="1vh">
      <img width="100%" :src="value" />
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    },
    loading: {
      type: Boolean
    }
  },
  data() {
    return {
      dialogVisible: false
    }
  },
  computed: {
    fileList: {
      get() {
        return this.value ? [{ url: this.value }] : []
      },
      set() {}
    }
  },
  methods: {
    handelDiaShow() {
      this.dialogVisible = true
    },
    handelRemove() {
      this.$emit('input', '')
    },
    // 上传
    handelUpload({ file }) {
      //   console.log(popObj)
      this.$emit('input', URL.createObjectURL(file))
      // 实例化cos
      const cos = new COS({
        SecretId: 'AKIDYrbAUXKr88uRlxFNKEwyvhCk36vxrXw4',
        SecretKey: 'Xr7D4i5E8nDME2e3NhphT5SZSGsFadBd'
      })
      // this.loading = true
      this.$emit('update:loading', true)
      cos.putObject(
        {
          Bucket: 'ashdata-ihrm-1317090674' /* 填入您自己的存储桶，必须字段 */,
          Region: 'ap-beijing' /* 存储桶所在地域，例如ap-beijing，必须字段 */,
          Key: file.name /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
          Body: file /* 必须，上传文件对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        },
        (err, data) => {
          if (err) {
            this.$message.success('上传失败')
          }
          // 拿到上传成功的头像地址同步到父组件中
          this.$emit('input', `https://${data.Location}`)
          // this.loading = false
          this.$emit('update:loading', false)
          this.$message.success('图片上传成功，可以提交')
          console.log(err || data)
        }
      )
    }
  }
}
</script>
<style lang="scss">
// .el-upload {
//   display: flex;
//   border: 1px dashed #d9d9d9;
//   border-radius: 6px;
//   cursor: pointer;
//   position: relative;
//   overflow: hidden;
// }

// .avatar-uploader-icon {
//   font-size: 28px;
//   color: #8c939d;
//   width: 178px;
//   height: 178px;
//   line-height: 178px;
//   text-align: center;
// }
// .avatar {
//   width: 178px;
//   height: 178px;
//   display: block;
// }
.el-upload {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 146px;
  height: 146px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.el-upload:hover {
  border-color: #409eff;
}

.el-icon-plus {
  font-size: 60px;
  color: #8c939d;
  text-align: center;
}
.disabled {
  .el-upload {
    display: none;
  }
}
</style>
